########################################################
# Project:    Talking to the Populist Radical Right
# Task:       The script merges the data sets with the
#             scores from the CA for the Danish parties, 
#             normalizes & annotates them
# Author:     Jan Schwalbach (21/07/2022)
########################################################

# Select legislative period and load respective data

load(file="CA_DK_ALL.Rdata")
load(file="CA_DK_MIG.Rdata")
load(file="CA_DK_ED.Rdata")

load(file="CA_DK_ALL_01.Rdata")
load(file="CA_DK_MIG_01.Rdata")
load(file="CA_DK_ED_01.Rdata")

load(file="CA_DK_ALL_05.Rdata")
load(file="CA_DK_MIG_05.Rdata")
load(file="CA_DK_ED_05.Rdata")

Positions_CA_DK_ALL[,1] <- (Positions_CA_DK_ALL[,1]-min(Positions_CA_DK_ALL[,1]))/(max(Positions_CA_DK_ALL[,1])-min(Positions_CA_DK_ALL[,1]))
Positions_CA_DK_ALL[,4] <- (Positions_CA_DK_ALL[,4]-min(Positions_CA_DK_ALL[,4]))/(max(Positions_CA_DK_ALL[,4])-min(Positions_CA_DK_ALL[,4]))
Positions_CA_DK_ALL[,7] <- (Positions_CA_DK_ALL[,7]-min(Positions_CA_DK_ALL[,7]))/(max(Positions_CA_DK_ALL[,7])-min(Positions_CA_DK_ALL[,7]))

Positions_CA_DK_MIG[,1] <- (Positions_CA_DK_MIG[,1]-min(Positions_CA_DK_MIG[,1]))/(max(Positions_CA_DK_MIG[,1])-min(Positions_CA_DK_MIG[,1]))
Positions_CA_DK_MIG[,4] <- (Positions_CA_DK_MIG[,4]-min(Positions_CA_DK_MIG[,4]))/(max(Positions_CA_DK_MIG[,4])-min(Positions_CA_DK_MIG[,4]))
Positions_CA_DK_MIG[,7] <- (Positions_CA_DK_MIG[,7]-min(Positions_CA_DK_MIG[,7]))/(max(Positions_CA_DK_MIG[,7])-min(Positions_CA_DK_MIG[,7]))

Positions_CA_DK_ED[,1] <- (Positions_CA_DK_ED[,1]-min(Positions_CA_DK_ED[,1]))/(max(Positions_CA_DK_ED[,1])-min(Positions_CA_DK_ED[,1]))
Positions_CA_DK_ED[,4] <- (Positions_CA_DK_ED[,4]-min(Positions_CA_DK_ED[,4]))/(max(Positions_CA_DK_ED[,4])-min(Positions_CA_DK_ED[,4]))
Positions_CA_DK_ED[,7] <- (Positions_CA_DK_ED[,7]-min(Positions_CA_DK_ED[,7]))/(max(Positions_CA_DK_ED[,7])-min(Positions_CA_DK_ED[,7]))

CA_DK_ALL_ALL <- Positions_CA_DK_ALL[,1:3]
CA_DK_ALL_GOV <- Positions_CA_DK_ALL[,4:6]
CA_DK_ALL_OPP <- Positions_CA_DK_ALL[,7:9]

CA_DK_MIG_ALL <- Positions_CA_DK_MIG[,1:3]
CA_DK_MIG_GOV <- Positions_CA_DK_MIG[,4:6]
CA_DK_MIG_OPP <- Positions_CA_DK_MIG[,7:9]

CA_DK_ED_ALL <- Positions_CA_DK_ED[,1:3]
CA_DK_ED_GOV <- Positions_CA_DK_ED[,4:6]
CA_DK_ED_OPP <- Positions_CA_DK_ED[,7:9]

names(CA_DK_ALL_ALL)[1] <- "Position"
names(CA_DK_ALL_GOV)[1] <- "Position"
names(CA_DK_ALL_OPP)[1] <- "Position"
names(CA_DK_MIG_ALL)[1] <- "Position"
names(CA_DK_MIG_GOV)[1] <- "Position"
names(CA_DK_MIG_OPP)[1] <- "Position"
names(CA_DK_ED_ALL)[1] <- "Position" 
names(CA_DK_ED_GOV)[1] <- "Position" 
names(CA_DK_ED_OPP)[1] <- "Position" 

names(CA_DK_ALL_ALL)[2] <- "Position2"
names(CA_DK_ALL_GOV)[2] <- "Position2"
names(CA_DK_ALL_OPP)[2] <- "Position2"
names(CA_DK_MIG_ALL)[2] <- "Position2"
names(CA_DK_MIG_GOV)[2] <- "Position2"
names(CA_DK_MIG_OPP)[2] <- "Position2"
names(CA_DK_ED_ALL)[2] <- "Position2" 
names(CA_DK_ED_GOV)[2] <- "Position2" 
names(CA_DK_ED_OPP)[2] <- "Position2" 

# Calculating the difference to the challenger party

CA_DK_ALL_ALL$challenger <- CA_DK_ALL_ALL$Position[1]
CA_DK_ALL_ALL$Difference <- CA_DK_ALL_ALL$Position-CA_DK_ALL_ALL$challenger
CA_DK_ALL_ALL$Difference <- abs(CA_DK_ALL_ALL$Difference)

CA_DK_ALL_GOV$challenger <- CA_DK_ALL_GOV$Position[1]
CA_DK_ALL_GOV$Difference <- CA_DK_ALL_GOV$Position-CA_DK_ALL_GOV$challenger
CA_DK_ALL_GOV$Difference <- abs(CA_DK_ALL_GOV$Difference)

CA_DK_ALL_OPP$challenger <- CA_DK_ALL_OPP$Position[1]
CA_DK_ALL_OPP$Difference <- CA_DK_ALL_OPP$Position-CA_DK_ALL_OPP$challenger
CA_DK_ALL_OPP$Difference <- abs(CA_DK_ALL_OPP$Difference)

CA_DK_MIG_ALL$challenger <- CA_DK_MIG_ALL$Position[1]
CA_DK_MIG_ALL$Difference <- CA_DK_MIG_ALL$Position-CA_DK_MIG_ALL$challenger
CA_DK_MIG_ALL$Difference <- abs(CA_DK_MIG_ALL$Difference)

CA_DK_MIG_GOV$challenger <- CA_DK_MIG_GOV$Position[1]
CA_DK_MIG_GOV$Difference <- CA_DK_MIG_GOV$Position-CA_DK_MIG_GOV$challenger
CA_DK_MIG_GOV$Difference <- abs(CA_DK_MIG_GOV$Difference)

CA_DK_MIG_OPP$challenger <- CA_DK_MIG_OPP$Position[1]
CA_DK_MIG_OPP$Difference <- CA_DK_MIG_OPP$Position-CA_DK_MIG_OPP$challenger
CA_DK_MIG_OPP$Difference <- abs(CA_DK_MIG_OPP$Difference)

CA_DK_ED_ALL$challenger <- CA_DK_ED_ALL$Position[1]
CA_DK_ED_ALL$Difference <- CA_DK_ED_ALL$Position-CA_DK_ED_ALL$challenger
CA_DK_ED_ALL$Difference <- abs(CA_DK_ED_ALL$Difference)

CA_DK_ED_GOV$challenger <- CA_DK_ED_GOV$Position[1]
CA_DK_ED_GOV$Difference <- CA_DK_ED_GOV$Position-CA_DK_ED_GOV$challenger
CA_DK_ED_GOV$Difference <- abs(CA_DK_ED_GOV$Difference)

CA_DK_ED_OPP$challenger <- CA_DK_ED_OPP$Position[1]
CA_DK_ED_OPP$Difference <- CA_DK_ED_OPP$Position-CA_DK_ED_OPP$challenger
CA_DK_ED_OPP$Difference <- abs(CA_DK_ED_OPP$Difference)

# Annotating government/opposition debates

CA_DK_ALL_ALL$type <-"all"
CA_DK_ALL_GOV$type <-"government"  
CA_DK_ALL_OPP$type <-"opposition"
CA_DK_MIG_ALL$type <-"all"
CA_DK_MIG_GOV$type <-"government"  
CA_DK_MIG_OPP$type <-"opposition"
CA_DK_ED_ALL$type <-"all" 
CA_DK_ED_GOV$type <-"government"   
CA_DK_ED_OPP$type <-"opposition" 

CA_DK_ALL_ALL$matter <-"all"
CA_DK_ALL_GOV$matter <-"all"  
CA_DK_ALL_OPP$matter <-"all"
CA_DK_MIG_ALL$matter <-"Migration"
CA_DK_MIG_GOV$matter <-"Migration"  
CA_DK_MIG_OPP$matter <-"Migration"
CA_DK_ED_ALL$matter <-"Education" 
CA_DK_ED_GOV$matter <-"Education"   
CA_DK_ED_OPP$matter <-"Education" 

Positions_DK <- rbind(CA_DK_ALL_ALL,CA_DK_ALL_GOV,CA_DK_ALL_OPP,CA_DK_MIG_ALL,CA_DK_MIG_GOV,CA_DK_MIG_OPP,CA_DK_ED_ALL,CA_DK_ED_GOV,CA_DK_ED_OPP)
Positions_DK$Party <- Positions_DK$party
Positions_DK$country <- "DK"
  
Positions_DK$Party[Positions_DK$Party == "S"] <- "SOC"
Positions_DK$Party[Positions_DK$Party == "V"] <- "VDK"

# left/right parties

Positions_DK$left <- 0
Positions_DK$left[Positions_DK$Party == "EL"] <- 1
Positions_DK$left[Positions_DK$Party == "RV"] <- 1
Positions_DK$left[Positions_DK$Party == "SOC"] <- 1
Positions_DK$left[Positions_DK$Party == "SF"] <- 1

# mainstream parties

Positions_DK$mainstream <- 0
Positions_DK$mainstream[Positions_DK$Party == "SOC"] <- 1
Positions_DK$mainstream[Positions_DK$Party == "VDK"] <- 1

# government/opposition parties and legislative period

Positions_DK$year <- 1998
Positions_DK$year <- 2001
Positions_DK$year <- 2005

Positions_DK$Government <- "Opposition"

Positions_DK$Government[Positions_DK$Party == "SOC" & Positions_DK$year == 1998] <- "Government"
Positions_DK$Government[Positions_DK$Party == "RV" & Positions_DK$year == 1998] <- "Government"
Positions_DK$Government[Positions_DK$Party == "SOC" & Positions_DK$year == 2011] <- "Government"
Positions_DK$Government[Positions_DK$Party == "RV" & Positions_DK$year == 2011] <- "Government"
Positions_DK$Government[Positions_DK$Party == "SF" & Positions_DK$year == 2011] <- "Government"
Positions_DK$Government[Positions_DK$Party == "VDK" & Positions_DK$year == 2001] <- "Government"
Positions_DK$Government[Positions_DK$Party == "KF" & Positions_DK$year == 2001] <- "Government"
Positions_DK$Government[Positions_DK$Party == "VDK" & Positions_DK$year == 2005] <- "Government"
Positions_DK$Government[Positions_DK$Party == "KF" & Positions_DK$year == 2005] <- "Government"
Positions_DK$Government[Positions_DK$Party == "VDK" & Positions_DK$year == 2007] <- "Government"
Positions_DK$Government[Positions_DK$Party == "KF" & Positions_DK$year == 2007] <- "Government"


save(Positions_DK, file = "Positions_DK_CA_normal.Rdata") 
save(Positions_DK, file = "Positions_DK_CA_01_normal.Rdata") 
save(Positions_DK, file = "Positions_DK_CA_05_normal.Rdata")
